<?php

function selectLimit($table,$field='*',$where=1,$order = 'order by Id desc',$limit=''){
	
//$conn = mysqli_connect('localhost', 'root', 'root','project', '3306');
$conn = mysqli_connect('localhost', '52484_fS9031', 'qjLY4HZmFiNtj55','52484dbKs2ss', '3306');
mysqli_query($conn, 'set names utf8');
$str = "select {$field} from {$table} where {$where} {$order} {$limit} " ;
//print_r($str);die;
$result = mysqli_query($conn, $str);
$arr = array();

while($re = mysqli_fetch_assoc($result))
{
	
	$arr[]=$re;
};
	
	return $arr;
	
};

//查询所有
function selectAll($table,$where=1,$field='*',$order = 'order by Id desc',$limit='')
{
	$str = "select {$field} from {$table} where {$where} {$order} {$limit} ";
//	print_r($str);
//	die;
	//获取数据库连接
//	$conn = mysqli_connect('localhost', 'root', 'root','project', '3306');
	$conn = mysqli_connect('localhost', '52484_fS9031', 'qjLY4HZmFiNtj55','52484dbKs2ss', '3306');
	mysqli_query($conn,'set names utf8');
	//要求连接执行sql语句，并获取结果集
	$result = mysqli_query($conn, $str);
	$arr = array();
	//获取所有值
	while($re = mysqli_fetch_assoc($result))
	{
		$arr[] = $re;
	}	
//	print_r($arr);
//	die;
	return $arr;
}


//删除语句封装
function delete($table,$where)
{
//	$conn = mysqli_connect('localhost', 'root', 'root','project', '3306');
	$conn = mysqli_connect('localhost', '52484_fS9031', 'qjLY4HZmFiNtj55','52484dbKs2ss', '3306');
	mysqli_query($conn,'set names utf8');
	$query = "delete from {$table} where Id={$where}";
	mysqli_query($conn, $query);
	//获取受影响行数
	$flag = mysqli_affected_rows($conn);
	//受影响行数大于0则删除成功，返回true
	if($flag>0)
	{
		return true;die;
	}
	return false;
}


//新增
function insert($table,$arr)
{
	$key = '';
	$val = '';
	foreach($arr as $k=>$v)
	{
		$key = $key.",".$k;
		$val = $val.",'".$v."'";
	}
	$key = substr($key, 1);
	$val = substr($val, 1);
	$str = "insert into {$table}({$key})values({$val})  ";
//	echo $str;
//	die;
	//获取数据库连接
//	$conn = mysqli_connect('localhost', 'root', 'root','project', '3306');
	$conn = mysqli_connect('localhost', '52484_fS9031', 'qjLY4HZmFiNtj55','52484dbKs2ss', '3306');
	mysqli_query($conn,'set names utf8');
	//要求连接执行sql语句，并获取结果集
	 mysqli_query($conn, $str);
	
	$flag = mysqli_affected_rows($conn);
	if($flag>0)
	{
		return true;die;
	}	
	return false;die;
}

function selectOne($table,$where=1,$field='*')
{
	$str = "select {$field} from {$table} where {$where}";
	//获取数据库连接
//	print_r($str);die;
//	$conn = mysqli_connect('localhost', 'root', 'root','project', '3306');
	$conn = mysqli_connect('localhost', '52484_fS9031', 'qjLY4HZmFiNtj55','52484dbKs2ss', '3306');
	mysqli_query($conn,'set names utf8');
	//要求连接执行sql语句，并获取结果集
	$result = mysqli_query($conn, $str);
	$arr = array();
	//获取所有值
	$re = mysqli_fetch_assoc($result);
//	print_r($re);
//	die;
	return $re;
}

function update($table,$abc,$id)
{
	$str = '';
	foreach($abc as $k=>$v)
	{
		$str = $str.','."$k='$v'";
	}
	$str = substr($str, 1);
	//获取数据库连接
//	$conn = mysqli_connect('localhost', 'root', 'root','project', '3306');
	$conn = mysqli_connect('localhost', '52484_fS9031', 'qjLY4HZmFiNtj55','52484dbKs2ss', '3306');
	mysqli_query($conn,'set names utf8');
	$query = "update $table set $str where Id=$id";
//	print_r($query);
//	die;
	//要求连接执行sql语句，并获取结果集
	 mysqli_query($conn, $query);
	
	$flag = mysqli_affected_rows($conn);
	if($flag>0)
	{
		return true;die;
	}	
	return false;die;
	
}

?>
